﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Text.RegularExpressions;
using DNA.Mvc.Text;
using DNA.Mvc.Notification;

namespace DNA.Mvc.Community.Events
{
    public class NewPostEventObserver : TemplatedMailEventObserver<NewPostEvent>
    {
        private readonly Regex AuthorRegex = new Regex("\\{author\\}", RegexOptions.Compiled);
        private readonly Regex ForumRegex = new Regex("\\{forum\\}", RegexOptions.Compiled);
        private readonly Regex SubjectRegex = new Regex("\\{subject\\}", RegexOptions.Compiled);
        private readonly Regex SummaryRegex = new Regex("\\{summary\\}", RegexOptions.Compiled);
        private readonly Regex PermalinkRegex = new Regex("\\{permalink\\}", RegexOptions.Compiled);
        private readonly Regex SenderRegex = new Regex("\\{sender\\}", RegexOptions.Compiled);

        public string Signature { get; set; }

        protected override string GetDestinationAddress(NewPostEvent e)
        {
            var user = Membership.GetUser(e.Post.Thread.Author);
            if (user != null)
                return user.Email;
            return "";
        }
        
        string body = "";

        protected override void OnFormatHtmlBody(NewPostEvent e, ref string html)
        {
            var user = Membership.GetUser(e.Post.UserName);
            html = AuthorRegex.Replace(html, user.GetDisplayName());
            html = ForumRegex.Replace(html, e.Post.Thread.Forum.Title);
            html = SubjectRegex.Replace(html, e.Post.Title);

            string summary = TextEngine.BBCode(e.Post.Body).ToHtmlString();
            html = SummaryRegex.Replace(html, summary);

            var permalink = GetPermalink(e);
            html = PermalinkRegex.Replace(html, permalink);

            if (!string.IsNullOrEmpty(Signature))
                html = SenderRegex.Replace(html, Signature);
            body = html;
        }

        private static string GetPermalink(NewPostEvent e)
        {
            var Url = DNA.Mvc.UrlUtility.CreateUrlHelper();
            var permalink =UrlResolver.ApplicationPath+ Url.Action("Index", "Thread", new { Area = "Community", id = e.Post.ThreadID });
            return permalink;
        }

        protected override void OnFormatTextBody(NewPostEvent e, ref string text)
        {
            var user = Membership.GetUser(e.Post.Thread.Author);

            var sb = new StringBuilder();
            sb.Append("Posted by : ")
                .Append(user.GetDisplayName())
                .AppendLine("- " + e.Post.Thread.Forum.Title)
                .AppendFormat("Subject: {0}", e.Post.Title)
                .AppendLine(TextEngine.Text(TextEngine.BBCode(e.Post.Body).ToHtmlString()))
                .AppendFormat("View the complete topic at : {0}", GetPermalink(e))
                .AppendLine()
                .AppendLine("Thanks");
            
            if (!string.IsNullOrEmpty(Signature))
                sb.AppendLine(",")
                    .Append(Signature);

            text = sb.ToString();
        }

        public override void Process(NewPostEvent e)
        {
            if (e.Post.IsApproved)
            {
                string content = ReadTemplateContent(e); ;

                if (!string.IsNullOrEmpty(Templated))
                    OnFormatHtmlBody(e, ref content);
                else
                    OnFormatTextBody(e, ref content);

                Notifier.Send(e.Post.SubscriptionToken, e.Post.Title, body);
            }
        }

    }
}
